# -*_ codeing=utf-8 -*-
# @Time: 2025/10/27 13:08
# @Author: foxhuty
# @File: db_main.py
# @Software: PyCharm
# @Based on python 3.10
from datetime import datetime
from sqlalchemy import create_engine,DateTime,func
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session

# future=True，表示只能用sqlalchemy2.0版本的样式
# engine = create_engine(r'sqlite:///huty_sqlite.db',echo=True,future=True)
# engine=create_engine(r'mysql+pymysql://root:foxmmer@localhost/db_school?charset=utf8',echo=True,future=True)
engine=create_engine(r'mysql+mysqldb://root:foxmmer@localhost:3306/db_students?charset=utf8',echo=True,future=True)
# engine=create_engine(r'mysql+mysqldb://root:foxmmer123@106.13.48.204:3306/db_fastapi?charset=utf8',echo=True,future=True)
# 定义一个模型类的基类

class BaseModel(DeclarativeBase):
    """
    所有的子模型都有的字段
    """
    create_time:Mapped[datetime]=mapped_column(DateTime,
                                               insert_default=func.now(),
                                               comment='记录的创建时间')
    update_time:Mapped[datetime]=mapped_column(DateTime,
                                               insert_default=func.now(),
                                               onupdate=func.now(),
                                               comment='记录最后一次修改的时间')

def get_session():
    session=Session(bind=engine)
    try:
        yield session
    finally:
        session.close()

import sqlalchemy_notes.student_manager.models